package com.hjli.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.dom4j.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hjli.model.ArticleCommentInfo;
import com.hjli.model.LinkInfo;
import com.hjli.model.PwdInfo;
import com.hjli.model.UserInfo;
import com.hjli.service.impl.CommentService;
import com.hjli.service.impl.LinkService;
import com.hjli.service.impl.PwdService;
import com.hjli.service.impl.UserService;
import com.hjli.tool.FileTool;
import com.hjli.tool.MD5Encryp;
import com.hjli.tool.PropertiesTool;
import com.hjli.tool.XMLTool;

@Controller
@RequestMapping("sys")
public class BackController {

	@Resource
	UserService userService;

	@Resource
	PwdService pwdService;

	@Resource
	LinkService linkService;

	@Resource
	CommentService commentService;

	/**
	 * 登录页面
	 * 
	 * @return
	 */
	@RequestMapping("login")
	public ModelAndView login() {
		ModelAndView model = new ModelAndView("login");
		return model;
	}

	/**
	 * 登录验证
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("login/validate")
	public @ResponseBody Map<String, Object> validate(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		if (request.getParameter("email") == null || request.getParameter("email").toString() == "") {
			resultMap.put("result", "账号不能为空");
			return resultMap;
		}
		if (request.getParameter("password") == null || request.getParameter("password").toString() == "") {
			resultMap.put("result", "密码不能为空");
			return resultMap;
		}
		UserInfo user = userService.findByEmail(request.getParameter("email").toString());
		if (user == null) {
			resultMap.put("result", "用户名不存在");
			return resultMap;
		}
		PwdInfo pwd = pwdService.findByDataId(user.getId());
		if (pwd != null) {

			if (pwd.getpPassword().equals(MD5Encryp.stringToMD5(request.getParameter("password")))) {
				HttpSession session = request.getSession();
				session.setAttribute("user", user);
				resultMap.put("result", "success");
			} else {
				resultMap.put("result", "用户名密码错误");
			}
		} else {
			resultMap.put("result", "用户名有误");
		}
		return resultMap;
	}

	/**
	 * 后台首页
	 * 
	 * @return
	 */
	@RequestMapping("index")
	public ModelAndView index() {
		ModelAndView model = new ModelAndView("admin/main/index");
		// 网站昵称
		model.addObject("siteName", "博客后台管理 | 李豪杰");
		return model;
	}

	/**
	 * 主题切换页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("themes")
	public ModelAndView themes(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView model = new ModelAndView("admin/main/theme/listTheme");
		String rootPath = request.getRealPath("/");
		String themePath = rootPath + "page/templates/themes";
		File file = new File(themePath);
		FileTool fileTool = new FileTool();
		List<String> list = fileTool.getFloderName(file);
		List<Map<String, Object>> themeMapList = new ArrayList<Map<String, Object>>();
		for (String str : list) {
			String xmlPath = str + "/about.xml";
			XMLTool xmlTool = new XMLTool();
			Element rootElement = xmlTool.xmlRoot(xmlPath);
			List<Element> xmlElementList = xmlTool.xmlChild(rootElement);
			Map<String, Object> map = new HashMap<String, Object>();
			for (Element el : xmlElementList) {
				map.put(el.getName().toString(), el.getData().toString());
			}
			themeMapList.add(map);
		}
		// 获取目前的主题昵称
		HttpSession session = request.getSession();
		String themeName = "";
		if (session.getAttribute("themeName") == null) {
			Properties property = PropertiesTool.getProperty("theme.properties");
			themeName = property.getProperty("themeName");
			session.setAttribute("themeName", themeName);
		} else {
			themeName = (String) session.getAttribute("themeName");
		}
		model.addObject("themeList", themeMapList);
		model.addObject("themeName", themeName);
		return model;
	}

	/**
	 * 文件管理页面
	 * 
	 * @return
	 */
	@RequestMapping("file")
	public ModelAndView file() {
		ModelAndView model = new ModelAndView("admin/main/file/listFile");
		return model;
	}

	/**
	 * 系统设置界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("set")
	public ModelAndView set(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView model = new ModelAndView("admin/main/set/view");
		// 基础设置参数
		Properties webproperty = PropertiesTool.getProperty("web.properties");
		Map<String, String> webValues = new HashMap<String, String>();
		webValues.put("webname", new String(webproperty.getProperty("webname").getBytes("ISO8859-1"), "UTF-8"));
		webValues.put("keywords", new String(webproperty.getProperty("keywords").getBytes("ISO8859-1"), "UTF-8"));
		webValues.put("description", new String(webproperty.getProperty("description").getBytes("ISO8859-1"), "UTF-8"));
		webValues.put("copyright", new String(webproperty.getProperty("copyright").getBytes("ISO8859-1"), "UTF-8"));

		// 七牛配置参数
		Properties qiniuProperty = PropertiesTool.getProperty("qiniu.properties");
		Map<String, String> qiniuValues = new HashMap<String, String>();
		qiniuValues.put("url", qiniuProperty.getProperty("URL").toString());
		qiniuValues.put("ak", qiniuProperty.getProperty("AK"));
		qiniuValues.put("sk", qiniuProperty.getProperty("SK"));
		qiniuValues.put("bn", qiniuProperty.getProperty("BN"));

		// 邮件配置参数
		Properties emailProperty = PropertiesTool.getProperty("email.properties");
		Map<String, String> emailValues = new HashMap<String, String>();
		emailValues.put("auth", emailProperty.getProperty("mail.smtp.auth"));
		emailValues.put("host", emailProperty.getProperty("mail.smtp.host"));
		emailValues.put("port", emailProperty.getProperty("mail.smtp.port"));
		emailValues.put("user", emailProperty.getProperty("mail.user"));
		emailValues.put("password", emailProperty.getProperty("mail.password"));

		// 邮件配置参数
		Properties tulingProperty = PropertiesTool.getProperty("tuling.properties");
		Map<String, String> tulingValues = new HashMap<String, String>();
		tulingValues.put("apikey", tulingProperty.getProperty("apikey"));
		tulingValues.put("secret", tulingProperty.getProperty("secret"));

		model.addObject("webValue", webValues);
		model.addObject("qiniuValue", qiniuValues);
		model.addObject("emailValue", emailValues);
		model.addObject("tulingValue", tulingValues);
		return model;
	}

	/**
	 * 友链管理界面
	 * 
	 * @return
	 */
	@RequestMapping("link")
	public ModelAndView list() {
		ModelAndView model = new ModelAndView("admin/main/link/listLink");
		List<LinkInfo> list = linkService.findAll();
		model.addObject("list", list);
		return model;
	}

	@RequestMapping("comment")
	public ModelAndView comment(){
		ModelAndView model = new ModelAndView("admin/main/comment/listComment");
		List<ArticleCommentInfo> list = commentService.findByPage(0, 10);
		int sumCount = commentService.findSumCount();
		int sumPageNum = 0;
		if(sumCount % 10 == 0){
			sumPageNum = sumCount / 10;
		}else{
			sumPageNum = sumCount / 10 + 1 ;
		}
		model.addObject("list",list);
		model.addObject("sumPageNum",sumPageNum);
		return model;
	}
}
